package com.chilli.cosima.action.customermanager;

import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.common.BussinessException;
import com.chilli.cosima.data.bean.Customer;
import com.chilli.cosima.service.CustomerService;

public class CustomerDelAction extends BaseSupport
{

	private static final long serialVersionUID = 2481391431028954035L;

	private long id;
	private CustomerService customerService;

	public String execute()
	{
		try
		{
			Customer customer = customerService.findCustomerById(id);
			customerService.deleteCustomer(id);
			if (null != customer.getPicture()
					&& !"".equals(customer.getPicture()))
			{
				File imageFile = new File(ServletActionContext
						.getServletContext().getRealPath(
								"/upload/images/customer")
						+ "/" + customer.getPicture());
				if (imageFile.exists())
				{
					imageFile.delete();
				}
			}
		} catch (BussinessException e)
		{
			logger.error("error", e);
		}

		return SUCCESS;
	}

	public long getId()
	{
		return id;
	}

	public void setId(long id)
	{
		this.id = id;
	}

	public CustomerService getCustomerService()
	{
		return customerService;
	}

	public void setCustomerService(CustomerService customerService)
	{
		this.customerService = customerService;
	}
}
